//
//  YMWebViewController.m
//  QrCode
//
//  Created by Andrew on 2018/6/22.
//  Copyright © 2018年 Andrew. All rights reserved.
//

#import "YMWebViewController.h"
#import <MBProgressHUD.h>
#import <MBProgressHUD+JDragon.h>

@interface YMWebViewController ()<UIWebViewDelegate>

/* webView */
@property (nonatomic, weak) UIWebView *webView;
/* MBProgressHUD */
@property (nonatomic, strong) MBProgressHUD *HUD;

#define SCREENWIDTH [UIScreen mainScreen].bounds.size.width
#define SCREENHEIGHT [UIScreen mainScreen].bounds.size.height

@end

@implementation YMWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 64, SCREENWIDTH, SCREENHEIGHT - 64)];
    webView.delegate = self;
    [self.view addSubview:webView];
    self.webView = webView;
    
    if (self.url.length) {
        NSURL *URL = [NSURL URLWithString:self.url];
        [self.webView loadRequest:[NSURLRequest requestWithURL:URL]];
    } else {
        NSURL *URL = [NSURL URLWithString:@"http://www.baidu.com"];
        [self.webView loadRequest:[NSURLRequest requestWithURL:URL]];
    }
}

/// 网页开始加载
- (void)webViewDidStartLoad:(UIWebView *)webView {
    
    [self GeneralButtonAction];
}

/// 网页完成加载
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    
    [_HUD removeFromSuperview];
}

/// 网页加载失败
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    [_HUD removeFromSuperview];
    [MBProgressHUD showInfoMessage:@"加载失败..."];
}

//   通常情况  文字  加 菊花
- (void)GeneralButtonAction{
    //初始化进度框，置于当前的View当中
    self.HUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:_HUD];
    
    //如果设置此属性则当前的view置于后台
    _HUD.dimBackground = YES;
    
    //设置对话框文字
    _HUD.labelText = @"加载中";
    //细节文字
    _HUD.detailsLabelText = @"请耐心等待";
    
    __weak typeof(self) weakSelf = self;
    //显示对话框
    [_HUD showAnimated:YES whileExecutingBlock:^{
        //对话框显示时需要执行的操作
        sleep(3);
    }// 在HUD被隐藏后的回调
       completionBlock:^{
           //操作执行完后取消对话框
           [weakSelf.HUD removeFromSuperview];
           weakSelf.HUD = nil;
       }];
}


@end
